package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class Signup extends Activity implements OnClickListener {
	EditText name;
	EditText password;
	EditText email;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);
		View signup_btn = findViewById(R.id.signup_button);
		signup_btn.setOnClickListener(this);
		name = (EditText) findViewById(R.id.signup_name);
		password = (EditText) findViewById(R.id.signup_pwd);
		email = (EditText) findViewById(R.id.signup_email);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_signup, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.signup_button:
			addUser(name.getText().toString(), password.getText().toString(), email.getText().toString());
			
			break;
		}
	}

	private void addUser(String name, String password, String mail) {
		// TODO Auto-generated method stub
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("username", name));
		postParameters.add(new BasicNameValuePair("password", password));
		postParameters.add(new BasicNameValuePair("email", mail));

		// String valid = "1";
		String response = null;
		try {
			
			response = CustomHttpClient.executeHttpPost(Constant.host
					+ "/UserController/addUser", postParameters);
			String res = response.toString();
			 res = res.trim();
			res = res.replaceAll("\\s+", "");
			// error.setText(res);
			if (Integer.parseInt(res)> 0) {
				Toast.makeText(this, "Đăng kí thành công",
						Toast.LENGTH_SHORT).show();
				//Constant.user = Integer.parseInt(res);
				startActivity(new Intent(this, Login.class));

			} else
				Toast.makeText(this, "Tài khoản này đã tồn tại. Đăng kí tài khoản khác",
						Toast.LENGTH_SHORT).show();

		} catch (Exception e) {

			
		}
		
	}
}
